<?php

namespace app\models;

use Yii;
use app\models\Customer;

/**
 * This is the model class for table "customer_collect".
 *
 * @property int $id
 * @property int $good_id 商品id
 * @property int $customer_id 用户id
 * @property int $is_collect 是否关注
 * @property int $created_at
 */
class CustomerCollect extends Base
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'customer_collect';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['good_id', 'customer_id', 'is_collect', 'created_at'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'good_id' => 'Good ID',
            'customer_id' => 'Customer ID',
            'is_collect' => 'Is Collect',
            'created_at' => 'Created At',
        ];
    }

    public function getCustomer(){
        return $this->hasOne(Customer::class, ['id' => 'customer_id']);
    }

    public function getGood(){
        return $this->hasOne(Good::class, ['id' => 'good_id']);
    }

    public static function isCollect($openid, $good_id){
        $is_collect = 0;
        if ($openid) {
            $customer = Customer::find()->where(['openid' => $openid])->one();
            if ($customer) {
                $is_collect = (int)self::find()->where(['customer_id' => $customer->id, 'good_id' => $good_id, 'is_collect' => 1])->exists();
            }
        }
        return $is_collect;
    }
}
